<?php
class WebsiteModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('db');
        $this->time = date("Y-m-d H:i:s",time());
    }
    
    public function selectSites(){
        $sql="select * from `sites`";
        return $this->db->query($sql);
    }

    public function getSitesInfo($start,$everyPage){
        $sql="select * from `sites` limit {$start},{$everyPage}";
        return $this->db->query($sql);
    }

    public function getAllCount()
    {
        $sql="select count(*) as `rows` from `sites` ";
        return $this->db->query($sql);
    }

    public function getSitesById($id){
        $sql="select * from `sites` where sites_id='{$id}'";
        return $this->db->query($sql);
    }

    public function del($id) 
    {
        $sql = "delete from `sites` where sid='{$id}' ";
        return $this->db->query($sql);
    }

    public function add($info) 
    {
        $sql="insert into `sites`(`name`,`url`,`create_time`,`ip`,`port_number`) values('{$info['name']}','{$info["url"]}',UNIX_TIMESTAMP('{$this->time}'),'{$info["ip"]}','{$info["port_number"]}')";
        return $this->db->query($sql);
    }

    public function edit($info, $id) 
    {
        //$sql = "update `sites` set `name`='{$info['name']}', `url`='{$info['url']}', `create_time`=UNIX_TIMESTAMP('{$this->time}'), `ip`='{$info['ip']}', `port_number`='{$info['port_number']}' WHERE sites_id='{$id}'";
        $sql = "update `sites` set `name`='{$info['name']}', `url`='{$info['url']}', `create_time`=UNIX_TIMESTAMP('{$this->time}'), `ip`='{$info['ip']}', `port_number`='{$info['port_number']}' WHERE sid='{$id}'";
        return $this->db->query($sql);
    }

    public function selectZhss() 
    {
        $sql = "select * from  category_zhonghuass";
        return $this->db->query($sql);
    }

    public function getInfoByArrparentId($id) 
    {
        $sql = "select * from  category_zhonghuass where arrparentid='{$id}'";
        //var_dump($sql);
        return $this->db->query($sql);
    }

    public function getZhssInfoByParentid($id) 
    {
        $sql = "select * from  category_zhonghuass where catid='{$id}'";
        return $this->db->query($sql);
    }

    public function selectSearchCategory($info) 
    {
        $name = trim($info['search_info']);
        $sql = "select catid,catname,parentid,child,arrchildid,arrparentid from `category_zhonghuass` where catname='{$name}'  ";
        //var_dump($this->db->query($sql));
        return $this->db->query($sql);
    }

    public function selectSearchCategoryParent($info) 
    {
        $result  = array();
        if(count($parent) < 3 || count($parent) == 3){
            $i = 0;
            foreach ($info as $key => $value) {
                if($value != 0 ){
                    $sql = "select catid,catname,parentid,child,arrchildid,arrparentid from `category_zhonghuass` where catid='{$value}'  ";
                    $result[$i++] = $this->db->query($sql)[0];
                } 
            }
        }
        else if(count($info) > 3){
            $i = 0;
            $reverse = array_reverse($info);
            $sql = "select catid,catname,parentid,child,arrchildid,arrparentid from `category_zhonghuass` where catid='{$reverse[0]}'  ";
            $result[$i++] = $this->db->query($sql)[0];
            $sql = "select catid,catname,parentid,child,arrchildid,arrparentid from `category_zhonghuass` where catid='{$reverse[1]}'  ";
            $result[$i++] = $this->db->query($sql)[0];
            array_multisort($result);
        }
        return $result;
    }

    public function selectSearchCategoryChild($info) 
    {
        $result  = array();
        $i = 0;
        foreach ($info as $key => $value) {
            $sql = "select catid,catname,parentid,child,arrchildid,arrparentid from `category_zhonghuass` where catid='{$value}'  ";
            if(count(explode(",",$this->db->query($sql)[0]['arrparentid'])) == 2){
                $result[$key] = $this->db->query($sql)[0];
            }
            else if(count(explode(",",$this->db->query($sql)[0]['arrparentid'])) == 3){
                $result[$key][$i++] = $this->db->query($sql)[0];
            } 
        }
        
        return $result;
    }

    public function selectSearchCategoryParChild($child,$catid) 
    {
        $result  = array();
        $i = 0;
        foreach ($child as $key => $value) {
        if($value !=0 && $value != $catid){
            $sql = "select catid,catname,parentid,child,arrchildid,arrparentid from `category_zhonghuass` where catid='{$value}'  ";
                if(!empty($this->db->query($sql)) ){
                    $result[$i++] = $this->db->query($sql)[0];
                }
            }
        }
        return $result;
    }
}
?>